package com.light.gulimall.product.web;

import com.light.gulimall.product.entity.CategoryEntity;
import com.light.gulimall.product.service.CategoryService;
import com.light.gulimall.product.vo.CategoryJsonVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;


    @RequestMapping({"/","index.html"})
    public String getIndex(Model model){
        //首页 查询所有一级分类
        log.info("开始查询所有一级分类");
        List<CategoryEntity> oneLevels = categoryService.getLevelOneCateGory();

        model.addAttribute("categorys",oneLevels);

        //springMVC自动拼接 classpath:/templates/+返回值+.html
        return "index";
    }

    /**
     * 首页渲染三级分类菜单
     *
     * @return 查询结果
     */
    @ResponseBody
    @RequestMapping("/index/catalog.json")
    public Map<String, List<CategoryJsonVO>> getCategoryJson(){
        return categoryService.getCategoryJson();
    }

}
